package cn.com.hbhgkj.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.hbhgkj.Constants;
import cn.com.hbhgkj.dao.CopyrightRegisterationDao;
import cn.com.hbhgkj.model.CopyrightRegisteration;
import cn.com.hbhgkj.service.ApplicationSendException;
import cn.com.hbhgkj.service.CopyrightRegisterationManager;
import cn.com.hbhgkj.service.SynchronizeException;
import cn.com.hbhgkj.util.CopyrightSender;
import cn.com.hbhgkj.util.DateUtil;

@Service("copyrightRegisterationManager")
@WebService(serviceName = "CopyrightRegisterationService", endpointInterface = "cn.com.hbhgkj.service.CopyrightRegisterationManager")
public class CopyrightRegisterationManagerImpl extends
		GenericManagerImpl<CopyrightRegisteration, Long> implements
		CopyrightRegisterationManager {
	CopyrightRegisterationDao copyrightRegisterationDao;

	public CopyrightRegisterationManagerImpl() {

	}

	@Autowired
	public CopyrightRegisterationManagerImpl(
			CopyrightRegisterationDao copyrightRegisterationDao) {
		super(copyrightRegisterationDao);
		this.copyrightRegisterationDao = copyrightRegisterationDao;
	}

	public void synchronize() throws SynchronizeException, ApplicationSendException{
		CopyrightSender.login(Constants.AGENT_USERNAME,
				Constants.AGENT_PASSWORD);
		Calendar date = Calendar.getInstance();
		date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
		
		List<CopyrightRegisteration> data = CopyrightSender.getData(
				DateUtil.getCurDate(),
				DateUtil.getDateTime("yyyy-MM-dd", date.getTime()));
		
		for (CopyrightRegisteration each : data) {
			if (!isSeriaNumExist(each.getSerialNum())) {
				copyrightRegisterationDao.save(each);
			}
		}
	}
	

	public boolean isSeriaNumExist(String seriaNum) {
		return copyrightRegisterationDao.findBySeriaNum(seriaNum) == null ? false
				: true;
	}

}